#!/usr/bin/env python

import sys, time, ConfigParser, os
from daemon import Daemon
from consciusblue import ConsciusBlueThread
from webservice import ConsciusWeb, WebThread
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

webhost=''
webport=8911

class ConsciusDaemon(Daemon):
	def run(self):
		configurationPath = os.path.expanduser('~/.consciusd.conf')
		if os.path.exists(configurationPath) and os.path.isfile(configurationPath):
			configuration = ConfigParser.SafeConfigParser()
			configuration.read(configurationPath)
		else:
			message = "configuration file %s does not exist. Try using the example configuration?\n"
			sys.stderr.write(message % configuration)
			self.stop()
			sys.exit(1)
			
		webhost = configuration.get('network', 'host', 0)
		webport = configuration.get('network', 'port', 0)

		try:
			WebThread().start()
			ConsciusBlueThread().start()
			while True:
				time.sleep(1)

		except KeyboardInterrupt:
			print "Somebody hit the CTRL+C button, why oh why?"
			#WebThread().exit()
